package com.zzs.team.Service;

import com.zzs.team.DAO.LikeDao;
import com.zzs.team.DAO.ReservationDao;
import com.zzs.team.DAO.TeamDao;
import com.zzs.team.Entity.Team;
import com.zzs.team.Entity.TeamShow;
import com.zzs.team.Utils.ReservationLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.PrivateKey;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class ReservationServiceImpl implements ReservationService {

    @Autowired
    private ReservationDao reservationDao;

    @Autowired
    private LikeDao likeDao;

    @Autowired
    private TeamDao teamDao;

    @Override
    public Map addReservation(String userID, Integer teamID) {

        Map<String,Integer> res = new HashMap<>();

        int index = teamID % 16;
        System.out.println("index:"+index);
        //映射对应的锁 （模仿ConcurrentHashMap）
        Object lock = ReservationLock.mylocks[index];

        synchronized (lock){
            //1、找到要预约的队伍信息
            Team reservation = reservationDao.getTeamByReserve(teamID);
            int allNumber = reservation.getAllNumber();//获取总人数人数限制（-1表示没有人数限制）
            int hasNumber = reservation.getHasNumber();//获取已有人数
            //2、1没有人数限制  或者 已有人数<总人数
            if(allNumber==-1 || hasNumber<allNumber){
                hasNumber++;
                reservationDao.addReservation(userID,teamID);
                teamDao.updateTeamhasNumber(teamID,hasNumber);
                //预约成功
                res.put("msg",1);
                res.put("hasNumber",hasNumber);
            }else{
                //预约失败 人数已满
                res.put("msg",0);
            }
        }
        return res;

    }

    @Override
    public Map cancleReservation(String userID, Integer teamID) {
        Map<String,Integer> res = new HashMap<>();
        //映射对应的锁 （模仿ConcurrentHashMap）
        Object lock = ReservationLock.mylocks[teamID%16];
        synchronized (lock){
            //1、找到要预约的队伍信息
            Team reservation = reservationDao.getTeamByReserve(teamID);
            int allNumber = reservation.getAllNumber();//获取总人数人数限制（-1表示没有人数限制）
            int hasNumber = reservation.getHasNumber();//获取已有人数
            //2、1没有人数限制  或者 已有人数<总人数
            if(allNumber==-1 || hasNumber<allNumber){
                hasNumber--;
                reservationDao.cancleReservation(userID,teamID);
                teamDao.updateTeamhasNumber(teamID,hasNumber);
                //取消预约成功
                res.put("msg",1);
                res.put("hasNumber",hasNumber);
            }else{
                //取消预约失败
                res.put("msg",0);
            }
        }
        return res;
    }

    @Override
    public List<TeamShow> getUserReservationsTeamShow(String userID) {
        List<TeamShow> reservations = reservationDao.getUserReservationsTeamShow(userID);
        List<Integer> likes = likeDao.getUserLikes(userID);
        //消去用户的id
        for(TeamShow reservation:reservations){
            for(int like:likes){
                if (like == reservation.getTeamID() ){
                   reservation.setLike(true);
                }
            }
            reservation.setReserve(true);
            reservation.setUserID(null);
            reservation.getUser().setOpenID(null);

        }
        return reservations;
    }
}
